﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BDH.PRE.Form
{
    using BDH.DAL.Entities;
    public partial class FrmNhatro : BDH.PRE.Form.FrmInput
    {
        BLL.QuanBLL _bllQuan = new BLL.QuanBLL();
        BLL.NhatroBLL _bllNhatro = new BLL.NhatroBLL();
        BLL.PhongBLL _bllPhong = new BLL.PhongBLL();
        DataTable _dtb_Nhatro;

        public FrmNhatro()
        {
            InitializeComponent();

            SetCaptionForm("Nhà trọ");

            btnSelect.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            btnPrint.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            //btnAdd.Caption = "Thêm nhà trọ";
            //btnEdit.Caption = "Cập nhật nhà trọ";
            //btnDelete.Caption = "Xóa nhà trọ";
        }

        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {
            if (!gvQuan.IsDataRow(gvQuan.FocusedRowHandle))
            {
                MessageBox.Show("Vui lòng chọn Đường", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            ClearDataBindings();
            ReadOnlyControl(false);
            ChangeStatus(false);
            ResetText();
            txtTenNhatro.Focus();

            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus();
            //PerformRefresh();
            Load_Nhatro();
            base.PerformCancel();
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            if (!gvNhatro.IsDataRow(gvNhatro.FocusedRowHandle))
            {
                MessageBox.Show("Vui lòng chọn Nhà trọ", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            var _id = Convert.ToInt32(gvNhatro.GetFocusedRowCellValue("id"));
            var ten_nt = gvNhatro.GetFocusedRowCellValue("id") + "";
            DialogResult result;
            result = MessageBox.Show("Bạn có chắc xóa "+ten_nt+" không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                var ok = _bllNhatro.Delete(_id);
                if (ok)
                {
                    //PerformRefresh();
                    Load_Nhatro();
                    MessageBox.Show("Xóa thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            txtTenNhatro.Focus();
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();
            ChangeStatus();
            ResetText();

            if (_dtb_Nhatro != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb_Nhatro.Rows.Count > 0) DataBindingControl();
            }

            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
            var tennt = Proper(txtTenNhatro.Text);
            var chunha = Proper(txtChunha.Text);

            if (IsAdd)
            {
                var o = new Nhatro()
                {
                    ten = tennt,
                    chunha = chunha,
                    diachi = txtDiachi.EditValue + "",
                    dienthoai = txtDienthoai.EditValue + "",
                    duong_id = Convert.ToInt64("0" + gvQuan.GetFocusedRowCellValue("duong_id"))
                };

                var oki = _bllNhatro.Insert(o);
                if (oki)
                {
                    //PerformRefresh();
                    Load_Nhatro();
                    ChangeStatus(false);
                    PerformAdd();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }

            }

            if (IsEdit)
            {
                var _id = Convert.ToInt32(gvQuan.GetFocusedRowCellValue("id"));

                var o = new Nhatro()
                {
                    id = _id,
                    ten = tennt,
                    chunha = chunha,
                    diachi = txtDiachi.EditValue + "",
                    dienthoai = txtDienthoai.EditValue + ""
                };

                var oki = _bllNhatro.Update(o);
                if (oki)
                {
                    //PerformRefresh();
                    Load_Nhatro();
                    ChangeStatus();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }

            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {
            txtTenNhatro.EditValue = null;
            txtChunha.EditValue = null;
            txtDiachi.EditValue = null;
            txtDienthoai.EditValue = null;

            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txtTenNhatro.DataBindings.Clear();
            txtChunha.DataBindings.Clear();
            txtDiachi.DataBindings.Clear();
            txtDienthoai.DataBindings.Clear();

            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {
            txtTenNhatro.DataBindings.Add("EditValue", _dtb_Nhatro, ".ten");
            txtChunha.DataBindings.Add("EditValue", _dtb_Nhatro, ".chunha");
            txtDiachi.DataBindings.Add("EditValue", _dtb_Nhatro, ".diachi");
            txtDienthoai.DataBindings.Add("EditValue", _dtb_Nhatro, ".dienthoai");

            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {

            txtTenNhatro.Properties.ReadOnly = isReadOnly;
            txtChunha.Properties.ReadOnly = isReadOnly;
            txtDiachi.Properties.ReadOnly = isReadOnly;
            txtDienthoai.Properties.ReadOnly = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            _dtb = _bllQuan.SelectAll();

            if (_dtb != null)
                dgQuan.DataSource = _dtb;

            base.LoadData();
        }
        #endregion

        private void gvQuan_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (!gvQuan.IsDataRow(gvQuan.FocusedRowHandle))
            {
                ClearDataBindings();
                ResetText();
                dgNhatro.DataSource = null;
                return;
            }
            Load_Nhatro();
        }

        private void Load_Nhatro()
        {
            int duong_id = Convert.ToInt32("0" + gvQuan.GetFocusedRowCellValue("duong_id"));
            _dtb_Nhatro = _bllNhatro.Select(duong_id);
            if (_dtb_Nhatro != null)
                dgNhatro.DataSource = _dtb_Nhatro;
        }

        private void gvNhatro_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            ClearDataBindings();
            ResetText();
            DataBindingControl();

            int nhatro_id = Convert.ToInt32("0" + gvNhatro.GetFocusedRowCellValue("id"));
            var dt = _bllPhong.Select(nhatro_id);
            if (dt != null)
                dgPhong.DataSource = dt;
            
        }

        private void btnAdd_Room_Click(object sender, EventArgs e)
        {
            if (!gvNhatro.IsDataRow(gvNhatro.FocusedRowHandle))
            {
                MessageBox.Show("Vui lòng chọn nhà trọ", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }                

            var frm = new Form.FrmPhong_Popup();
            frm.id_nhatro = Convert.ToInt32("0" + gvNhatro.GetFocusedRowCellValue("id"));
            frm.IsAdd = true; frm.IsEdit = false;
            frm.Size = new Size(800, 600);
            frm.StartPosition = FormStartPosition.CenterScreen;
            frm.ShowDialog();

            gvNhatro_FocusedRowChanged(sender, null);
        }

        private void btnEdit_Room_Click(object sender, EventArgs e)
        {
            if (!gvPhong.IsDataRow(gvPhong.FocusedRowHandle))
            {
                MessageBox.Show("Nhà trọ chưa có phòng, \n Vui lòng thêm phòng trước", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            var frm = new Form.FrmPhong_Popup();
            frm.id_nhatro = Convert.ToInt32("0" + gvNhatro.GetFocusedRowCellValue("id"));
            frm.id_phong = Convert.ToInt32("0" + gvPhong.GetFocusedRowCellValue("id"));
            frm.IsAdd = false; frm.IsEdit = true;
            frm.Size = new Size(800, 600);
            frm.StartPosition = FormStartPosition.CenterScreen;
            frm.ShowDialog();

            gvNhatro_FocusedRowChanged(sender, null);
        }

        private void btnEdit_Dongia_Click(object sender, EventArgs e)
        {
            if (!gvNhatro.IsDataRow(gvNhatro.FocusedRowHandle))
            {
                MessageBox.Show("Vui lòng chọn nhà trọ", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }   

            var frm = new FrmDongia_Popup();
            frm.id_nhatro = Convert.ToInt32("0" + gvNhatro.GetFocusedRowCellValue("id"));
            frm.Size = new Size(800, 600);
            frm.StartPosition = FormStartPosition.CenterScreen;
            frm.ShowDialog();

            gvNhatro_FocusedRowChanged(sender, null);
        }

        private void btnXoaphong_Click(object sender, EventArgs e)
        {
            if (!gvPhong.IsDataRow(gvPhong.FocusedRowHandle))
            {
                MessageBox.Show("Vui lòng chọn phòng", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            var id_phong = Convert.ToInt32(gvPhong.GetFocusedRowCellValue("id"));
            var ten_phong = gvPhong.GetFocusedRowCellValue("id") + "";
            DialogResult result;            
            result = MessageBox.Show("Bạn có chắc xóa phòng "+ten_phong+" không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                var ok = _bllPhong.Delete(id_phong);
                if (ok)
                {
                    //PerformRefresh();
                    Load_Nhatro();
                    MessageBox.Show("Xóa thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

       


      
    }
}
